package com.example.onetomanydemo.mapper;/*
 **
 * @author yangyang
 *@date 2022/06/29 21:51
 **/

import com.example.onetomanydemo.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserMapper {

    /*
     * 一对一查询
     * property：查询结果赋值给此实体属性
     * column：对应数据库的表字段，做为下面@One(select方法的查询参数
     * one：一对一的查询
     * @One(select = 方法全路径) ：调用的方法
     */
     @Select("SELECT * FROM user WHERE id = #{id}")
     @Results({
       @Result(property = "company",column = "company_id",one = @One(select = "com.example.onetomanydemo.mapper.CompanyMapper.getCompanyById"))
     })
     User getUserWithCompany(Long id);

    /*
     * 一对多查询
     * property：查询结果赋值给此实体属性
     * column：对应数据库的表字段，可做为下面@One(select方法)的查询参数
     * many：一对多的查询
     * @Many(select = 方法全路径) ：调用的方法
     */
      @Select("SELECT * FROM user WHERE id = #{id}")
      @Results({
          @Result(property = "accounts",column = "id",many = @Many(select = "com.example.onetomanydemo.mapper.AccountMapper.getAccountByUserId"))
      })
     User getUserWithAccount(Long id);

    /*
     * 同时用一对一、一对多查询
     */

    @Select("SELECT * FROM user")
    @Results({
        @Result(property = "id",column = "id"),
        @Result(property = "company",column = "company_id",one = @One(select = "com.example.onetomanydemo.mapper.CompanyMapper.getCompanyById")),
        @Result(property = "accounts",column = "id",many = @Many(select = "com.example.onetomanydemo.mapper.AccountMapper.getAccountByUserId"))
    })
   List<User>  getAll();


}
